import { defineStore } from 'pinia';
import router from '@/router';
const useMenuStore = defineStore('menu', {
	state: () => {
		return {
			userInfo:uni.getStorageSync('userInfo') || {}
		};
	},
	actions: {
		setMenu(username: string, permission: string[],token:string) {
			this.userInfo.permitUrl = permission;
			this.userInfo.username = username;
			this.userInfo.tree = router;
			this.userInfo.defaultAct = '0';
			this.userInfo.defaultOpen = [];
			this.userInfo.pageTitle = '首页';
			this.userInfo.isCollapse = false;
			this.userInfo.token = token;
			this.userInfo.flattenTree = this.flattenMenuTree(router)
			uni.setStorageSync('userInfo',this.userInfo)
		},

		// 将tree转为一维数组
		 flattenMenuTree (menutree:typeof router):string[]{
			let result = [];
			for (let i = 0; i < menutree.length; i++) {
				let node = menutree[i];
				if (node.children && node.children.length > 0) {
					let children = this.flattenMenuTree(node.children);
					result = result.concat(children);
				}
				result.push(node);
			}
			return result;
		}
	}
});
export default useMenuStore;


